/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gallery_ability_slice.h"

#include "ability_env.h"
#include "ability_manager.h"
#include "gfx_utils/file.h"
#include "securec.h"

namespace OHOS
{
    REGISTER_AS(GalleryAbilitySlice)

    GalleryAbilitySlice::~GalleryAbilitySlice()
    {
        printf("~GalleryAbilitySlice() | start \n");
        Clear();
        printf("~GalleryAbilitySlice() | end \n");
    }

    void GalleryAbilitySlice::Clear()
    {
        printf("GalleryAbilitySlice::Clear\n");

        if (camStateMng != nullptr && isStart)
        {
            isStart = false;
            camStateMng->Stop();
            printf("------>> camera %s \r\n", isStart ? "start" : "stop");
            delete camStateMng;
        }
    
        // delete surfaceView;
        // delete cameraLabel_;
        // delete backLabel_;
        // delete backIcon_;
    }

    void GalleryAbilitySlice::OnStart(const Want &want)
    {
        printf("GalleryAbilitySlice::OnStart V7\n");
        AbilitySlice::OnStart(want);

        //用于相机预览
        // surfaceView = new UISurfaceView();
        // surfaceView->SetPosition(0, 0, 960, 480);
        // surfaceView->GetSurface()->SetWidthAndHeight(1920, 1080);

        //顶层view配置: 位置、大小、背景色
        rootView_ = RootView::GetWindowRootView();
        rootView_->SetPosition(0, 0);
        rootView_->Resize(960, 480);

        //返回箭头: 位置、图片、不透明度
        backIcon_ = new UIImageView();
        backIcon_->SetPosition(0, 0, 80, 80);
        char tempPath[512] = {0};
        sprintf_s(tempPath, sizeof(tempPath),
            "%s/gallery/assets/gallery/resources/drawable/ic_back.png", GetSrcPath());
        backIcon_->SetSrc(tempPath);
        // backIcon_->SetStyle(STYLE_BACKGROUND_OPA, 0);
        backIcon_->SetTouchable(true);

        //退出提示: 位置、文字居中、字体和内容
        backLabel_ = new UILabel();
        backLabel_->SetPosition(80, 0, 80, 80);
        // backLabel_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
        backLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
        backLabel_->SetFont("SourceHanSansSC-Regular.otf", 25);
        backLabel_->SetText("返回");
        backLabel_->SetTouchable(true);

        //启动摄像头: 位置、文字居中、字体和内容
        cameraLabel_ = new UILabel();
        cameraLabel_->SetPosition(100, 100, 100, 80);
        // cameraLabel_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
        cameraLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
        cameraLabel_->SetFont("SourceHanSansSC-Regular.otf", 25);
        cameraLabel_->SetText("摄像头");
        cameraLabel_->SetTouchable(true);

        //按键回调
        auto backOnClick = [this] (UIView& view, const Event& event) -> bool {
            printf("############  backOnClick  #############\n");
            //终结ability
            TerminateAbility();
            printf("############  backOnClick  #############\n");
            return true;
        };
        backListener_ = new EventListener(backOnClick, nullptr);
        backIcon_->SetOnClickListener(backListener_);
        backLabel_->SetOnClickListener(backListener_);

        auto cameraOnClick = [this] (UIView& view, const Event& event) -> bool {
            printf("############  cameraOnClick  #############\n");
            if (camStateMng != nullptr)
            {
                if (isStart)
                    camStateMng->Stop();
                else
                    camStateMng->StartPreview();
                isStart = !isStart;
            }
            printf("------>> camera %s \r\n", isStart ? "start" : "stop");
            printf("############  cameraOnClick  #############\n");
            return true;
        };
        cameraListener_ = new EventListener(cameraOnClick, nullptr);
        cameraLabel_->SetOnClickListener(cameraListener_);

        //添加子view
        // rootView_->Add(surfaceView);
        rootView_->Add(backIcon_);
        rootView_->Add(backLabel_);
        rootView_->Add(cameraLabel_);

        //显示view
        SetUIContent(rootView_);
    }

    void GalleryAbilitySlice::OnInactive()
    {
        printf("GalleryAbilitySlice::OnInactive\n");
        AbilitySlice::OnInactive();
    }

    void GalleryAbilitySlice::OnActive(const Want &want)
    {
        printf("GalleryAbilitySlice::OnActive\n");
        AbilitySlice::OnActive(want);

        if (camStateMng == nullptr)
        {
            //创建kit
            camKit = CameraKit::GetInstance();
            if (camKit != nullptr)
            {
                string camId;
                list<string> camList = camKit->GetCameraIds();
                for (auto &cam : camList) {
                    cout << "camera name:" << cam << endl;
                    const CameraAbility *ability = camKit->GetCameraAbility(cam);
                    // find camera which fits user's ability
                    list<CameraPicSize> sizeList = ability->GetSupportedSizes(0);
                    for (auto &pic : sizeList) {
                        cout << "resolution: " << pic.width << "x" << pic.height << " -- 1920x1080" << endl;
                        if (pic.width == 1920 && pic.height == 1080) {
                            camId = cam;
                            break;
                        }
                    }
                }
                if (!camId.empty())
                {
                    //创建实例
                    camStateMng = new SampleCameraStateMng(eventHdlr);
                    camKit->CreateCamera(camId, *camStateMng, eventHdlr);
                }
                else
                    cout << "No available camera.(1080p wanted)" << endl;
            }
            else
                cout << "Can not get CameraKit instance" << endl;
        }
    }

    void GalleryAbilitySlice::OnBackground()
    {
        printf("GalleryAbilitySlice::OnBackground\n");
        AbilitySlice::OnBackground();
    }

    void GalleryAbilitySlice::OnStop()
    {
        printf("GalleryAbilitySlice::OnStop\n");
        AbilitySlice::OnStop();
        Clear();
    }
}
